package viewCollection;

import java.awt.BorderLayout;




import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.TreeMap;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;



import javax.swing.table.TableCellRenderer;

import util.Messages;
import viewCollection.TraitsView.TraitHandler;
import dataCollection.Card;
import dataCollection.Card.Suit;
import dataCollection.Card.Value;
import dataCollection.CardDeck;
import dataCollection.Dice;
import dataCollection.Die;
import dataCollection.InvalidDiceException;

public class CardTraitDialog extends JDialog {
  private static final long serialVersionUID = 114722264014898389L;
  public TreeMap<Card, JokerInfoWrapper> map_;
  public JButton ok_button_;
  public JButton delete_button_;
  public JTable  table_;
  boolean allow_to_close_;
  
  public CardTraitDialog () {
    allow_to_close_ = false;
    map_ = new TreeMap<Card, JokerInfoWrapper>();

    DefaultTableModel model = new DefaultTableModel() {
      @Override
      public boolean isCellEditable(int row, int column) {
         if(column == 0) return false;
         if(((Card)this.getValueAt(row, 0)).getValue().equals(Value.JOKER))
           return true;
         return false;
      } 
    };
    model.addColumn(Messages.getString("TraitDialogCard"));
    model.addColumn(Messages.getString("TraitDialogDice"));
    CardDeck cardDeck = new CardDeck();
    cardDeck.shuffle();

    for(int i = 0; i < 12; i++){
      Card card = cardDeck.takeUpcard();
      if(card.getValue().equals(Value.JOKER)) {
        Card suit_card = cardDeck.takeUpcard();
        if(suit_card.getValue().equals(Value.JOKER)) {
          // take another one
          suit_card = cardDeck.takeUpcard();
        }
        Card current_joker = new Card(suit_card.getSuit(), Value.JOKER);
        card = current_joker;
        map_.put(card, new JokerInfoWrapper(card));
      }
      try {
        Dice dice = null;
        if(!card.getValue().equals(Value.JOKER))
          dice = new Dice(card.getLevel(), card.getGroup());
        Object[] row = {card, dice};
        if(dice != null)
          System.out.println("NEW DICE: " + dice.toString());
        else
          System.out.println("NEW DICE: " + "NULL (JOKER)");
        model.addRow(row);
      } catch(InvalidDiceException exc) {
        System.out.println(card.toString());
        exc.printStackTrace();
      }
    }
    finalizeDialog(model);
  }
  
  void finalizeDialog(DefaultTableModel model) {
    ok_button_ = new JButton(Messages.getString("OK"));
    delete_button_ = new JButton(Messages.getString("Remove"));
    table_ = new CardDiceTable(model);

    table_.setRowHeight(25);
    setModal(true);
    setLayout(new BorderLayout());
    setTitle(Messages.getString("TraitDialogTitle"));
    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    ok_button_.setEnabled(false);
    add(ok_button_, BorderLayout.EAST);
    add(delete_button_, BorderLayout.WEST);
    add(new JScrollPane(table_), BorderLayout.NORTH);
    this.pack();
  }
  
  public void setAllowToClose(boolean b) {
    allow_to_close_ = b;
  }
  
  public boolean getAllowToClose() {
    return allow_to_close_;
  }
  
  class JokerInfoWrapper {
    Card card_;
    public JComboBox<Dice> box_;
    public DefaultCellEditor editor_;
    
    public JokerInfoWrapper(Card card) {
      card_ = card;
      int level = card_.getLevel();
      try {
      Dice[] dice = { new Dice(level, 4), new Dice(level,6), new Dice(level,8), new Dice(level,10), new Dice(level,12)};
      
      box_ = new JComboBox<Dice>(dice);
      box_.addMouseListener(new MouseAdapter() {
        public void mouseExited(MouseEvent e) {
          if(box_.getSelectedItem() == null) {
            ok_button_.setEnabled(false);
          } else {
            ok_button_.setEnabled(allow_to_close_);
          }
        }
      }
      );

      editor_ = new DefaultCellEditor(box_);
      } catch(InvalidDiceException e) {
        e.printStackTrace();
      }
    }
  }
  
  class ComboBoxCellRenderer implements TableCellRenderer {
    JComboBox<Dice> box_;
    public ComboBoxCellRenderer(JComboBox<Dice> box) {
      box_ = box;
      setName("Table.cellRenderer");
    }

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column) {
      setBackground(isSelected?table.getSelectionBackground()
          :table.getBackground());
      if(value!=null) {
        box_.setSelectedItem(value);
      }
      return box_;
    }

  }
  
  class CardDiceTable extends JTable {
    /**
     * 
     */
    private static final long serialVersionUID = -3214079967890520208L;

    public CardDiceTable(DefaultTableModel model) {
      super(model);
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
      if(column == 0) return super.getCellRenderer(row, column);
      DefaultTableModel model = (DefaultTableModel)this.getModel();
      Card c = (Card)model.getValueAt(row, 0);
      if(c.getValue().equals(Value.JOKER)) {
        return new ComboBoxCellRenderer(map_.get(c).box_);
      }
      return super.getCellRenderer(row, column);
    }

    @Override
    public TableCellEditor getCellEditor(int row, int column) {
      if(column == 0) return super.getCellEditor(row, column);
      DefaultTableModel model = (DefaultTableModel)this.getModel();
      Card c = (Card)model.getValueAt(row, 0);
      if(c.getValue().equals(Value.JOKER)) {
        return map_.get(c).editor_;
      }
      System.out.println("Row ELSE: " + row);
      return super.getCellEditor(row, column);
    } 
  }
} 



